import React, { PureComponent } from "react";
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import codePush from "react-native-code-push";
import TestPage from './src/pages/TestPage';
import DebugPage from './src/pages/DebugPage';
import ControllPage from './src/pages/ControllPage';
import RegisterDevicePage from './src/pages/RegisterDevicePage';
import DeviceRunningSummaryPage from './src/pages/DeviceRunningSumaryPage';
import DeviceInfoPage from './src/pages/DeviceInfoPage';
import SingleHolePage from './src/pages/SingleHolePage';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { RootSiblingParent } from 'react-native-root-siblings';
import CodePushUpgradeDialog from './src/common/Widgets/CodePushDialog';
import LocalCabinetControllPage from "./src/pages/LocalCabinetControlPage";
import WelcomePage from "./src/pages/WelcomePage";
import MainLogicLoop from "./src/logic/Main";
import ErrorRecoveryManager from "./src/logic/ErrorRecoveryManager";
import DebugManager from "./src/logic/DebugManager";
import StrategyManager from "./src/logic/StrategyManager";
import DebugDevicePage from "./src/pages/DebugDevicePage";

let codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };

const Stack = createNativeStackNavigator();

class App extends PureComponent {
  componentDidMount() {
    this.updateTimer = setInterval(()=>{
      this.codePushRef && this.codePushRef.checkUpdate();
    }, 1000 * 60 * 30);
  }

  componentWillUnmount() {
    this.updateTimer && clearInterval(this.updateTimer);
    this.updateTimer = null;
    StrategyManager.uninstallStrategyLogic();
    MainLogicLoop.uninstallMainLoop();
    ErrorRecoveryManager.uninstallErrorRecoveryManager();
    DebugManager.uninstallDebugManager();
  }

  render() {
    return <SafeAreaProvider>
            <RootSiblingParent>
              <RouterList />
            </RootSiblingParent>
            <CodePushUpgradeDialog ref={(ref)=>{this.codePushRef = ref}}/>
        </SafeAreaProvider>
  }
}

function RouterList() {
  return (
    <NavigationContainer>
      <Stack.Navigator screenOptions={{ headerShown: false, statusBarHidden: true }} initialRouteName={'Welcome'}>
        <Stack.Screen name="Home" component={TestPage} />
        <Stack.Screen name="Welcome" component={WelcomePage} />
        <Stack.Screen name="Main" component={LocalCabinetControllPage}/>
        <Stack.Screen name="Colony" component={ControllPage}/>
        <Stack.Screen name="RegisterDevice" component={RegisterDevicePage}/>
        <Stack.Screen name="DeviceSummary" component={DeviceRunningSummaryPage}/>
        <Stack.Screen name="DeviceInfo" component={DeviceInfoPage}/>
        <Stack.Screen name="SingleHole" component={SingleHolePage}/>
        <Stack.Screen name="Debug" component={DebugPage} />
        <Stack.Screen name="DebugDevice" component={DebugDevicePage} />
        {/* <Stack.Screen name="MQTTServer" component={MQTTServerTestPage} />
        <Stack.Screen name="MQTTClient" component={MQTTClientTestPage} />
        <Stack.Screen name="Rs485Test" component={Rs485TestPage} /> */}
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default codePush(codePushOptions)(App);